// pages/coupon/coupon.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: 0,
    noData: false,
    isEmpty: false,
    isExchangePage: 1,
    isUsedPage: 1,
    number: 10,
    couponList: [],
    userScore: 0
  },

  typeChange: function (e) {
    this.setData({
      key: e.currentTarget.dataset.key
    })
    if (this.data.key == 0) {
      app.post('Coupon/index').then(res => {
        
        this.setData({
          couponList: res.data.result_data,
          isEmpty: res.data.result_data.length == 0
        })
      })
    } else {
      app.post('Coupon/coupon', {
        token: wx.getStorageSync('token'),
        state: this.data.key - 1,
        page: 1,
        number: this.data.number
      }).then(res => {
        if (res.data.result_data.length == 0) {
          this.setData({
            isEmpty: true,
            bottomText: ''
          })
        }
        this.setData({
          couponList: res.data.result_data
        })
      })
    }
  },

  exchange: function (e) {
    wx.showLoading({
      title: '',
    })
    app.post('Coupon/exchange', {
      token: wx.getStorageSync('token'),
      id: e.currentTarget.dataset.cid
    }).then(res => {
      wx.hideLoading()
      wx.showModal({
        content: res.data.result_message,
        showCancel: false,
        confirmColor: '#5bac26',
        success: res => {
          if (res.confirm) {
            this.onLoad()
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.post('Coupon/index').then(res => {
      this.setData({
        couponList: res.data.result_data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('到底了')
    console.log(this.data.isExchangePage);
    console.log(this.data.isUsedPage);
    
    if (this.data.noData) {
      this.setData({
        bottomText: '没有更多了'
      })
    } else {
      wx.showLoading({
        title: '正在加载...',
      })
      app.post('Coupon/coupon', {
        token: wx.getStorageSync('token'),
        state: this.data.key - 1,
        page: this.data.key == 1 ? this.data.isExchangePage + 1 : this.data.isUsedPage + 1,
        number: this.data.number
      }).then(res => {
        if (this.data.key == 1) {
          this.data.isExchangePage++
        } else {
          this.data.isUsedPage++
        }
        if (res.data.result_data.length == 0) {
          this.setData({
            // isEmpty: true,
            noData: true
          })
        }
        this.setData({
          couponList: this.data.couponList.concat(res.data.result_data)
        })
      })
      wx.hideLoading()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
